
import Toast from '../../miniprogram_npm/_vant-weapp@0.5.7@vant-weapp/toast/toast'; //消息提示组件
import { getWithPage, getapply } from '../../config/https'  //提现页面数据， 提现到微信
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    withData:'',      //下单页面
    minPrice:'', //可提现金额
    value: "", //提现金额
  },
  //正则表达式 判断输入框的值
  vlue(e) {
    console.log(!(/[a-zA-z\u4e00-\u9fa5]/g.test(e.detail.value)))
    //if (!(/[a-zA-z\u4e00-\u9fa5]/g.test(e.detail.value))) {//全局匹配 若是有字母就为“否” 

      
   // if (/^(\d?)+(\.\d{0,2})?$/.test(e.detail.value)){
      if (e.detail.value<=0){
        this.setData({ value: "", })
      }else{
        this.setData({ value: e.detail.value })
      } 
    //}else{
      // wx.showToast({
      //   title: "请输入最多两位小数点金额或正确格式！！！",
      //   icon: 'none'
      // })
    //   this.setData({ value: e.detail.value })
    // }

    // } else {
    //   this.setData({value: "",})
    //   //console.log("金额输入格式错误")
    // }
  },
  focus(){ //输入框聚焦时
    //this.setData({ onholder:''})
  }, 
  onBlur(){//失去焦点
    //this.setData({ onholder: this.data.can_withdraw})
  }, 
  buton() { //点击提现的时候
    let that=this;
    let value= that.data.value; //输入的提现金额
    let can_withdraw= that.data.can_withdraw; //可用提现金额(余额)
    console.log(can_withdraw, value)
    let minPrice = that.data.minPrice;   //最小提现金额
    const appid= wx.getAccountInfoSync().miniProgram.appId; //小程序appid账号

    if (/^(\d?)+(\.\d{0,2})?$/.test(value)) { //输入最多两位小数点
      console.log(value > Number(can_withdraw))
      if (value > Number(can_withdraw)) {
        Toast("余额已不足")
      } else if (value=="") {
        Toast("不能为空")
      } else if (value<minPrice) {
        Toast("不能小于可提现金额")
      } else {
        wx.showLoading({ title: '请稍后' })//加载提示动画
        getapply({  //提现接口
          money: value
        }).then(res => { //成功
          wx.hideLoading() //加载提示动画消失
          Toast(res.message == undefined?"提现已申请": res.message)
          var num = 2;
          let timer = setInterval(() => {
            num--;
            num == 0 ? clearInterval(timer) : '';
            num == 0 ? wx.navigateBack({ delta: 1 }) : '';
          }, 1000)
          num = num
        }).catch(err => {//失败
          wx.hideLoading() //加载提示动画消失
          Toast(err.message == undefined ? "提现申请失败" : res.message)
          //wx.navigateBack({ delta: 1 })
        })

      } 
    
        
    }else{
      wx.showToast({
        title: "请输入正确格式！！！",
        icon: 'none'
      })
    }


   
 
   
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) { //onholder:options.can_withdraw
    this.setData({ can_withdraw: options.can_withdraw, value: options.can_withdraw }) //可用提现金额
    console.log('更新')
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    let that = this
    getWithPage({//提现页面数据
    }).then(res => {
      console.log(res)
      that.setData({
        minPrice: res.withdraw_min_price
      })
    }) 
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})